package kr.co.ham.control;

import java.awt.Component;

import javax.swing.ButtonModel;
import javax.swing.JRadioButton;

import kr.co.ham.view.Variables;

import org.apache.log4j.Logger;

/**
 * @see 변환 컨트롤 
 * @author hamjjang
 * @since 2013. 05. 05
 * @version 1.0
 */
public class ChangeControlImpl implements ChangeControl{
	public static Logger logger = Logger.getLogger("filelog");
	public static FileService fileService = new FileServiceImpl(); 
	@Override
	public void exit() {
		// TODO Auto-generated method stub
		logger.debug("ChangeControlImpl.exit()");
		System.exit(0);
	}
	@Override
	public void getPath(String gubun, Component obj) {
		// TODO Auto-generated method stub
		logger.debug("ChangeControlImpl.getPath()");
		String filePath = fileService.getFileOpen(obj);
		
		if("0".equals(gubun)){
			Variables.edt_orgPath.setText(filePath);
		}else if("1".equals(gubun)){
			Variables.edt_newPath.setText(filePath);
		}
	}
	@Override
	public boolean isVaridate()
	{
		String strOrgPath = Variables.edt_orgPath.getText().trim(); 
		String strNewPath = Variables.edt_newPath.getText().trim();
		String strOption  = Variables.selectOption; 
		String strUsrDef  = Variables.edt_userDef.getText().trim();
		
		if("".equals(strOrgPath) || strOrgPath.isEmpty()){
			Variables.jOptionPane.showMessageDialog(null, "원본 파일의 경로가 없습니다. 입력해 주세요.");
			Variables.edt_orgPath.setFocusable(true);
			return false;
		}
		
		if("".equals(strNewPath) || strNewPath.isEmpty()){
			Variables.jOptionPane.showMessageDialog(null, "변경 파일의 경로가 없습니다. 입력해 주세요.");
			Variables.edt_newPath.setFocusable(true);
			return false;
		}
		
		if("N".equals(strOption) || strOption.isEmpty()){
			Variables.jOptionPane.showMessageDialog(null, "변경 옵션을 선택해 주세요.");
			return false;
		}
		
		if("U".equals(strOption) && ("".equals(strUsrDef) || strUsrDef.isEmpty()) ){
			Variables.jOptionPane.showMessageDialog(null, "사용자지정 옵션은 사용자지정명을 입력해주세요.");
			Variables.edt_userDef.setFocusable(true);
			return false;
		}
		
		return true;
		
	}
	@Override 
	public void setChange() {
		// TODO Auto-generated method stub
		String strOrgPath = Variables.edt_orgPath.getText().trim(); 
		String strNewPath = Variables.edt_newPath.getText().trim();
		String strOption  = Variables.selectOption; 
		String strUsrDef  = Variables.edt_userDef.getText().trim();
		
		fileService.setFileNmChange(strOrgPath, strNewPath, strOption, strUsrDef);
	}
	@Override
	// 초기화
	public void setClear() {
		// TODO Auto-generated method stub
		logger.debug("ChangeControlImpl.setClear()");
		Variables.edt_orgPath.setText("");
		Variables.edt_newPath.setText("");
		Variables.edt_userDef.setText("");
		Variables.txt_stat.setText("");
		
		Variables.edt_userDef.setEditable(false);
		
		Variables.rdo_crateDtm.setSelected(true);
		Variables.rdo_modDtm.setSelected(false);
		Variables.rdo_userDef.setSelected(false);
		Variables.rdo_etc.setSelected(false);
		
		Variables.selectOption = "N";
		
		
	}
	@Override
	public void setOption(JRadioButton obj) {
		// TODO Auto-generated method stub
		boolean select = obj.isSelected();
        String strOption = "" ;
        Variables.edt_userDef.setEditable(false);
        if(select){
        	if(Variables.rdo_crateDtm == obj){
				strOption = "C";
        	}else if(Variables.rdo_modDtm == obj){
				strOption = "M";
        	}else if(Variables.rdo_userDef == obj){
				strOption = "U";
				Variables.edt_userDef.setEditable(true);
				Variables.edt_userDef.setFocusable(true);
        	}else if(Variables.rdo_etc == obj){
				strOption = "E";
        	}else{
				strOption = "N";
			}
        }
        
		logger.debug("  strOption [" + strOption + "]") ;
		Variables.selectOption = strOption ;
	}
	@Override
	public void setDelectOrgFiles() {
		// TODO Auto-generated method stub
		String strOrgPath = Variables.edt_orgPath.getText().trim(); 
		boolean ret = fileService.setDelectOrgFiles(strOrgPath);
		if(ret){
			Variables.jOptionPane.showMessageDialog(null, "원본 파일 삭제에 성공하였습니다.");
		}
	}

}
